package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;

import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName MenusController
 * @Author wangqiuxiang
 * @Date 2019/7/5 19:57
 * @Description TODO
 * @Version 1.0
 **/
@RestController
@RequestMapping("/menu")
public class MenusController {
    @Reference
    private MenuService menuService;

/**
    *@Author wangqiuxiang
    *@Description //TODO 添加菜单
    *@Date 21:15 2019/7/5
    *@Param [menu]
    *@return com.itheima.entity.Result
    **/
    @RequestMapping("/add")
    private Result add(@RequestBody Menu menu,Integer[]ids){
        try {
            if (ids!=null&&ids.length>0){
            menu.setParentMenuId(ids[0]);}
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }
/**
    *@Author wangqiuxiang
    *@Description //TODO 查询所有菜单回显到页面
    *@Date 21:15 2019/7/5
    *@Param []
    *@return com.itheima.entity.Result
    **/
@RequestMapping("/findAll")
    public  Result findAll(){
    try {
        List<Menu>list=menuService.findAll();
        return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_MENU_FAIL);
    }
}

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
    PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(),
            queryPageBean.getPageSize());
    return pageResult;


}
/**
    *@Author wangqiuxiang
    *@Description //TODO 编辑菜单
    *@Date 22:11 2019/7/5
    *@Param [menu]
    *@return com.itheima.entity.Result
    **/
@RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
    try {
        menuService.edit(menu);
        return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.EDIT_MENU_FAIL);
    }
}

/**
    *@Author wangqiuxiang
    *@Description //TODO 根据id查询回显数据
    *@Date 22:11 2019/7/5
    *@Param [id]
    *@return com.itheima.entity.Result
    **/
    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id) {
        try {
            Menu menu = menuService.findMenuById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch(Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    /**
        *@Author wangqiuxiang
        *@Description //TODO 删除
        *@Date 22:12 2019/7/5
        *@Param [id]
        *@return com.itheima.entity.Result
        **/
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }
        catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }
    @RequestMapping("/findParentMenuById")
    public Result findParentMenuById(Integer id){
        Integer parentId=menuService.findParentId(id);
        if (parentId==null){

            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS);
        }else {
            List<Integer> list = new ArrayList<>();
            Menu menu= menuService.findMenuById(parentId);
            list.add(menu.getId());
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }


    }






}
